<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <el-card shadow="always">
          <org-tree ref="orgTree" modelType="'dept','community'" @nodeClick="handleNodeClick" @firstNodeClick="handleFirstNodeClick"/>
        </el-card>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-card shadow="always">
          <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
            <el-row :gutter="20">
              <el-col :span="6" :xs="24">
                <el-form-item label="路线名称" prop="pathName">
                  <el-input
                    v-model="queryParams.pathName"
                    placeholder="请输入路线名称"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"/>
                </el-form-item>
              </el-col>
              <el-col :span="6" :xs="24">
                <el-form-item label="巡检人员" prop="nickName">
                  <el-input
                    v-model="queryParams.nickName"
                    placeholder="请输入巡检人员"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"/>
                </el-form-item>
              </el-col>
              <el-col :span="6" :xs="24">
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['waitress:path:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['waitress:path:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['waitress:path:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['waitress:path:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="pathList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="路线名称" align="center" prop="pathName" />
              <el-table-column label="巡检人员" align="center" prop="nickName" />
              <el-table-column label="巡检开始时间" align="center" prop="crulserBegin"/>
              <el-table-column label="巡检结束时间" align="center" prop="crulserEnd"/>
              <el-table-column label="巡逻类型" align="center" prop="crulserType" >
              <template slot-scope="scope">
                  {{ dict.label.crulser_type[scope.row.crulserType] }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['waitress:path:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['waitress:path:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"/>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <fieldset>
          <legend>路线</legend>
          <el-row :gutter="20">
            <el-col :span="8" :xs="24">
              <el-form-item label="所属小区" prop="communityId">
                <el-select v-model="form.communityId" style="width: 100%;" placeholder="请选择所属小区">
                  <el-option
                    v-for="dict in communityOptions"
                    :key="dict.id"
                    :label="dict.communityName"
                    :value="dict.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="路线名称" prop="pathName">
                <el-input v-model="form.pathName" placeholder="请输入路线名称" :disabled="disabled"/>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="巡检人员" prop="crulserId">
                <el-select v-model="form.crulserId" style="width: 100%;" placeholder="请选择巡检人员">
                  <el-option
                    v-for="dict in userOptions"
                    :key="dict.userId.toString()"
                    :label="dict.nickName"
                    :value="dict.userId.toString()"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="巡检开始时间" prop="crulserBegin">
                <el-time-picker
                  v-model="form.crulserBegin"
                  format="HH:mm"
                  value-format="HH:mm"
                  :picker-options="{}"
                  placeholder="任意时间点">
                </el-time-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="巡检结束时间" prop="crulserEnd">
                <el-time-picker
                  v-model="form.crulserEnd"
                  format="HH:mm"
                  value-format="HH:mm"
                  :picker-options="{}"
                  placeholder="任意时间点">
                </el-time-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="巡检类型">
                <el-radio-group v-model="form.crulserType" :disabled="disabled">
                  <el-radio
                    v-for="item in dict.crulser_type"
                    :key="item.dictValue"
                    :label="item.dictValue"
                  >{{item.dictLabel}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" :xs="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
              </el-form-item>
            </el-col>
          </el-row>
        </fieldset>
        <fieldset>
          <legend>地点</legend>
          <PathDetailA ref="detail" :key="PathDetailAKey" :communityId="this.form.communityId" :pathId="this.form.id"/>
        </fieldset>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listMixin } from "@/utils/mixins/listMixin";
import { listPath, delPath, exportPath, getPath , addPath, updatePath } from "@/api/waitress/path";
import { listCommunity,getCommunity} from "@/api/property/community";
import { listUser } from "@/api/system/user";
import  PathDetailA   from "../PathDetail"
export default {
  name: "Path",
  components: {PathDetailA},
  mixins:[ listMixin ],
  dicts: ['crulser_type'],
  data() {
    return {
      PathDetailAKey:0,
      // 路线管理表格数据
      form:{},
      pathList: [],
      currentNode:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否禁用
      disabled: false,
      communityOptions:[],
      userOptions:[],
      deptId:null,
      routeParam:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        pathName: null,
        crulserId: null,
        crulserBegin: null,
        crulserEnd: null,
        crulserType: null,
        nodeId:null,
        nickName:null,
      },
      // 表单校验
      rules: {
        communityId: [
          { required: true, message: "小区ID不能为空", trigger: "blur" }
        ],
        pathName: [
          { required: true, message: "路线名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch:{
    'form.communityId'(v){
      getCommunity(this.form.communityId).then(res=>{
        listUser({'deptId':res.data.deptId}).then(respon =>{
          this.userOptions =respon.rows;
        })
      })
    }
  },
  created() {
    // this.getList();
    listCommunity().then(res=>{
      this.communityOptions = res.rows;
    });
  },
  methods: {
    /** 查询路线管理列表 */
    getList() {
      this.loading = true;
      listPath(this.queryParams).then(response => {
        this.pathList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("cancel");
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if(this.currentNode.modelType == 'community'){
        this.form.communityId = this.currentNode.id
        this.open = true;
      }else {
        this.open = true;
      }
      this.PathDetailAKey++;
      this.title = "新增路线管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      const option = {
          "type":"UPDATE",
          "id": id
      }
      getPath(option.id).then(response => {
        this.form = response.data;
        if(option.type == 'VIEW'){
          this.title = "查看路线管理";
        }else{
          this.title = "修改路线管理";
        }
        this.open = true;
        this.PathDetailAKey++;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        pathName: null,
        crulserId: null,
        crulserBegin: null,
        crulserEnd: null,
        crulserType: "20",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        ancestors: null,
        pathDetailList:[],
      };
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除路线管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPath(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    handleNodeClick(node){
      this.currentNode = node;
      this.queryParams.nodeId = node.id
      this.getList()
    },
    handleFirstNodeClick(node){
      this.currentNode = node;
      this.queryParams.nodeId = node.id;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有路线管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPath(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let pathDetailList=this.$refs.detail.getDetailList();
            if (pathDetailList && pathDetailList.length >0){
              this.form.pathDetailList = pathDetailList;
            }
          if (this.form.id != null) {
            updatePath(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.cancel()
            });
          } else {
            addPath(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.cancel()
            });
          }
        }
      });
    }
  }
};
</script>
